//-----------------------------------------------------------------------------
// File: ObjectActivator.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;

namespace ObjectiveDraw.Platform
{
	/// <summary>
	/// Summary description for ObjectActivator.
	/// </summary>
	public class ObjectActivator : Cerebrum.Integrator.Activator, Cerebrum.IFormatter
	{
		public static readonly Cerebrum.ObjectHandle ObjectiveDrawCustomObjectSerialTypeId = new Cerebrum.ObjectHandle(Cerebrum.Runtime.KernelSerialTypeId.CustomObject.ToInt64() + 32);

		public ObjectiveDraw.Platform.Application DrawerApplication;

		System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

		public ObjectActivator()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IFormatter Members

		public void Serialize(Cerebrum.IConnector connector, System.IO.Stream stream, object component)
		{
			Cerebrum.IPersistent persistent;

			if(component!=null && this.DrawerApplication.m_CustomTypes.ContainsKey(component.GetType()))
			{
				Cerebrum.ObjectHandle.WriteTo(stream, ObjectActivator.ObjectiveDrawCustomObjectSerialTypeId);
				string typeName = Convert.ToString(this.DrawerApplication.m_CustomTypes[component.GetType()]);
				this.formatter.Serialize(stream, typeName);
			}
			else
			{
				Cerebrum.ObjectHandle.WriteTo(stream, Cerebrum.Runtime.KernelSerialTypeId.SystemObject);
				this.formatter.Serialize(stream, component);
			}
			if (component != null) 
			{
				persistent = component as Cerebrum.IPersistent;
				if (persistent != null)
					persistent.Serialize(Cerebrum.SerializeDirection.Save, connector, stream);
			}
		}

		public object Deserialize(Cerebrum.IConnector connector, System.IO.Stream stream)
		{
			object component;
			Cerebrum.ObjectHandle typeTag;

			typeTag = Cerebrum.ObjectHandle.ReadFrom(stream);
			if (typeTag == Cerebrum.Runtime.KernelSerialTypeId.SystemObject)
			{
				component = this.formatter.Deserialize(stream);
			}
			else 
				if (typeTag == ObjectActivator.ObjectiveDrawCustomObjectSerialTypeId)
			{
				string typeName = Convert.ToString(this.formatter.Deserialize(stream));
				if(this.DrawerApplication.m_CompBackTypeMap.ContainsKey(typeName))
				{
					typeName = Convert.ToString(this.DrawerApplication.m_CompBackTypeMap[typeName]);
				}
				component = ObjectiveDraw.Platform.Workspace.CreateInstanceByTypeName(this.DrawerApplication.DomainContext, typeName);
			}
			else
			{
				throw new System.Exception("Wrong KernelSerialTypeId = " + typeTag.ToString());
			}


			if (component != null) 
			{
				if(connector!=null)
				{
					Cerebrum.IComponent local2;
					local2 = component as Cerebrum.IComponent;
					if (local2 != null)
					{
						local2.SetConnector(Cerebrum.SerializeDirection.Load, connector);
					}
				}
				long local3;
				local3 = stream.Length;
				if (stream.Position < local3) 
				{
					Cerebrum.IPersistent persistent;
					persistent = component as Cerebrum.IPersistent;
					if (persistent != null)
					{
						persistent.Serialize(Cerebrum.SerializeDirection.Load, connector, stream);
					}
				}
			}
			return component;
		}

		#endregion
	}
}
